using System;
using System.Diagnostics;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.Core
{
    internal class WIQueriesRoot : WIObject
    {
        public WIQueriesRoot( WIProjectProvider parent ) : base( parent )
        {
            this.InitFolder( this.Parent.Folder );
            this.InitChilds();
        }

        public WIProjectProvider ProjectProvider
        {
            get { return (this.Parent as WIProjectProvider); }
        }

        public Uri ProjectUri
        {
            get { return this.ProjectProvider.ProjectUri; }
        }
        public string ProjectName
        {
            get { return this.ProjectProvider.ProjectName; }
        }
        public Project GetProject()
        {
            return this.ProjectProvider.GetProject();
        }

        public override WIObjectType Type
        {
            get { return WIObjectType.QueriesRoot; }
        }
        public override string FolderName
        {
            get { return Resources.WORK_ITEMS_FOLDER_NAME; }
        }

        private void InitChilds()
        {
            lock( this.Childs )
            {
                foreach( QueryScope scope in Enum.GetValues( typeof( QueryScope ) ) )
                {
                    if( !this.Childs.ContainsKey(scope) )
                        this.Childs.Add( scope, new WIQueriesGroup(this, scope) );
                }
            }
        }

        protected override void UpdateData()
        {
 	        this.Folder.Name = Resources.WORK_ITEMS_FOLDER_NAME;

            lock( this.Childs )
            {
                foreach( QueryScope scope in Enum.GetValues( typeof(QueryScope) ) )
                {
                    WIQueriesGroup group = null;

                    if( !this.Childs.ContainsKey(scope) )
                    {
                        group = new WIQueriesGroup( this, scope );
                        this.Childs.Add( scope, group );
                    }
                    else
                    {
                        group = this.Childs[scope] as WIQueriesGroup;
                    }

                    try
                    {
                        group.Update();
                    }
                    catch( System.Exception ex )
                    {
                        Debug.WriteLine( ex.ToString() );
                    }
                }
            }
        }

        public WIQueriesGroup this[QueryScope scope]
        {
            get
            {
                WIQueriesGroup result = null;

                lock( this.Childs )
                {
                    if( this.Childs.ContainsKey( scope ) )
                        result = this.Childs[scope] as WIQueriesGroup;
                }

                return result;
            }
        }

        public override OlFolderView FolderView
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    }
}
